home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 555 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  87 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Q: default constructor for fundamental types?
  5. Date: 26 Feb 1996 20:13:44 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4gsvhn$csi@clarknet.clark.net>
  9. References: <4glrdc$dqo@ra.ibr.cs.tu-bs.de>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Mime-Version: 1.0
  12. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  13. Content-Transfer-Encoding: 8bit
  14. X-Nntp-Posting-Host: explorer.clark.net
  15. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  16. Originator: clamage@taumet
  17.  
  18. Dirk Herrmann (dirk@sallust.ida.ing.tu-bs.de) wrote:
  19. : Sorry if this has been discussed before.
  20. : When using templates, it appears that there should be
  21. : default constructors for the fundamental data types.
  22.  
  23. Essentially, there is.
  24.  
  25. : Example:
  26. : template <class T>
  27. : X {
  28. :   public:
  29. :     X() : t() {};
  30. :     X(const T& tt) : t(tt) {};
  31. :   private:
  32. :     T t;
  33. : }
  34. : As far as i know, the default constructor of
  35. : e.g. X<int> would not compile since there is no default
  36. : constructor for an int value.
  37. : It seems to be a pity that this would not be allowed.
  38.  
  39. But it appears that it is allowed. It works fine in Visual C++ 2.2. 
  40. There's are just two things: you left out the word "class" between "template 
  41. <class T>" and the word "X"; and you need a semicolon after the closing 
  42. right brace of the class definition.
  43.  
  44. The following produces output of "8" on the first line and gobbledygook 
  45. on the second line, since the effect of the "default constructor" of the 
  46. integer member function is to do nothing.
  47.  
  48. If your X class were only meant for numeric types, you could have the 
  49. default constructor of X call the constructor for t as t(0) instead of 
  50. t() if you always wanted default initialization to 0.
  51.  
  52. #include <iostream.h>
  53.  
  54. template <class T>
  55.  X {
  56.   public:
  57.     X() : t() {};
  58.     X(const T& tt) : t(tt) {};
  59.      void output() { cout << t << "\n"; }
  60.   private:
  61.      T t;
  62. };
  63.  
  64. int main() 
  65. {
  66.     X<int> i(8);
  67.     X<int> j;
  68.     i.output();
  69.     j.output();
  70.     int x;
  71.     cin >> x; // Pause for input: this is a QuickWin program.
  72.     return 0;
  73. }
  74.  
  75.  
  76. [ To submit articles: Try just posting with your newsreader.
  77.               If that fails, use mailto:std-c++@ncar.ucar.edu
  78.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  79.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  80.   Comments? mailto:std-c++-request@ncar.ucar.edu
  81. ]
  82.